#include "WindowAppFramework.h"

WindowAppFramework::WindowAppFramework()
	: m_hWnd(0),
	m_windowMode(0),
	m_windowColor(0),
	m_windowWidth(0),
	m_windowHeight(0),
	m_screenWidth((float)GetSystemMetrics(SM_CXSCREEN)),
	m_screenHeight((float)GetSystemMetrics(SM_CYSCREEN)),
	m_windowed(false)
{
	//emtpy
}

WindowAppFramework::~WindowAppFramework()
{
	//empty
}

void WindowAppFramework::init(
	char*	      a_windowName,
	int		      a_windowColor,
	float         a_windowWidth,
	float         a_windowHeight,
	bool	      a_windowed,
	WindowHandle* a_windowHandler)
{
	//store the window width, height, and mode for ease of access
	m_windowColor  = a_windowColor;
	m_windowWidth  = a_windowWidth;
	m_windowHeight = a_windowHeight;
	m_windowed     = a_windowed;

	//fill the window class information
	ZeroMemory(&m_wClass,sizeof(WNDCLASSEX));
	m_wClass.cbSize			= sizeof(WNDCLASSEX);
	m_wClass.lpfnWndProc	= (WNDPROC)a_windowHandler->m_wndProc;
	m_wClass.hInstance		= a_windowHandler->m_hInstance;
	m_wClass.hCursor		= LoadCursor(0,IDC_ARROW);
	m_wClass.lpszClassName	= TEXT("WindowClass");
	m_wClass.hbrBackground	= (HBRUSH)COLOR_WINDOW;

	//set the window mode properties base on whether it is in window mode or not
	//if it is window mode, add the chrome width to it to prvent scaling issues
	int width = (int)m_windowWidth, height = (int)m_windowHeight;
	if(m_windowed)
	{
		m_windowMode |= (WS_OVERLAPPEDWINDOW) & ~(WS_MINIMIZEBOX|WS_MAXIMIZEBOX|WS_THICKFRAME);
		width  += WIN_CHROME_WIDTH;
		height += WIN_CHROME_HEIGHT;
	}
	else
		m_windowMode |= (WS_EX_TOPMOST|WS_POPUP);

	//center window
	m_windowCoordX = (int)((m_screenWidth/2)-(width/2));
	m_windowCoordY = (int)((m_screenHeight/2)-(height/2));

	//create the window
	RegisterClassEx(&m_wClass);
	m_hWnd = CreateWindowEx(
		0,m_wClass.lpszClassName,
		TEXT(a_windowName),
		m_windowMode,
		m_windowCoordX,
		m_windowCoordY,
		width,height,0,0,
		a_windowHandler->m_hInstance,0);

	//show and update the window
	ShowWindow(m_hWnd, a_windowHandler->m_nCmdShow);
	UpdateWindow(m_hWnd);
}

bool WindowAppFramework::hMsg()
{
	static MSG msg;
	if(PeekMessage(&msg,0,0,0,PM_REMOVE))
	{
		//return false if the "X" button is hit
		if(msg.message == WM_QUIT)
			return false;
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return true;
}